package tablature;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Class: BarLine
 * Description: Holds a bar line of notes.
 * @author et
 */
public class BarLine implements Serializable
{
    private int barLength;


    public static final Character REST = '-';

    private ArrayList <Character> line;

    private String componentName;

    private TimeSignature timeSignature;

    /**
     * Constructor for bar line.
     * @param componentName - the component name in this bar line
     * @param timeSignature - the time signature for this bar line.
     */
    public BarLine(String componentName, TimeSignature timeSignature)
    {
        this.componentName = componentName;
        this.timeSignature = timeSignature;

        barLength = timeSignature.getBeatsPerMeasure() * timeSignature.getBeatSize();

        line = new ArrayList <Character> (barLength);
        

        // If the name is blank, then it's a timing line.
        if (componentName.equals(""))
        {
            String barString = timeSignature.getTimingLine();
            for (int i = 0; i < barLength; i++)
            {
                line.add(barString.charAt(i));
            }
        }
        else
        {
            for (int i = 0; i < barLength; i++)
            {
                line.add(REST);
            }
        }
    }

    /**
     * Gets the name of the component for this bar line.
     * @return the component name
     */
    public String getName()
    {
        return componentName;
    }

    /**
     * Gets the note at a given position in the bar line.
     * @param pos - the position in the bar line.
     * @return the note at that position.
     */
    public Character getNote(int pos)
    {
        return line.get(pos);
    }

    /**
     * Sets the note at a given position.
     * @param pos - the position
     * @param note - the new note.
     */
    public void setNote(int pos, Character note)
    {
        line.set(pos, note);
    }

    /**
     * Gets the length of this bar line.
     * @return the length of the bar line.
     */
    public int getLength()
    {
        return line.size();
    }

    /**
     * Gets the time signature for this bar line.
     * @return timeSignature - the time signature for this bar line.
     */
    public TimeSignature getTimeSignature()
    {
        return timeSignature;
    }

    /**
     * Gets the string representation of this bar line.
     * @return bar - the bar line as a string.
     */
    public String toString()
    {
        String bar = "";


        for (int i = 0; i < barLength; i++)
        {
            bar += line.get(i);
        }
        return bar;
    }
}
